package com.wangrui.test;

import com.wangrui.dao.UserMapperDynamic;
import com.wangrui.entity.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class MyBatisTestForDynamicSql {

    /**
     * 获得MyBatis SqlSessionFactory
     * SqlSessionFactory负责创建SqlSession，一旦创建成功，就可以用SqlSession实例来执行映射语句
     * ，commit，rollback，close等方法。
     *
     * @return
     */
    private static SqlSessionFactory getSessionFactory() {
        SqlSessionFactory sessionFactory = null;
        String resource = "configuration.xml";
        try {
            sessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsReader(resource));
        } catch (IOException e) {

            e.printStackTrace();
        }
        return sessionFactory;
    }

    @Test
    public void testForEach() {
        SqlSessionFactory factory = getSessionFactory();
        SqlSession session = factory.openSession();
        UserMapperDynamic userMapper = session.getMapper(UserMapperDynamic.class);
        List<Integer> ids = new ArrayList<>();
        ids.add(1);
        ids.add(2);
        for (int i = 0; i < 10000; i++) {
            ids.add(19785 + i);
        }
        List<User> userList = userMapper.getUserByInCondition(ids);
        userList.forEach(e -> System.out.println(e));
        session.close();
    }

    @Test
    public void testForWhere(){
        SqlSessionFactory factory = getSessionFactory();
        SqlSession session = factory.openSession();
        UserMapperDynamic userMapper = session.getMapper(UserMapperDynamic.class);
        User userCondition = new User();
        userCondition.setAge(23);
        List<User> userList = userMapper.whereCondition(userCondition);
        System.out.println(userList);
    }

    @Test
    public void testForTrim(){
        SqlSessionFactory factory = getSessionFactory();
        SqlSession session = factory.openSession();
        UserMapperDynamic userMapper = session.getMapper(UserMapperDynamic.class);
        User userCondition = new User();
        userCondition.setName("wangrui");
        userCondition.setAge(23);
        List<User> userList = userMapper.trimCondition(userCondition);
        System.out.println(userList);
    }

    @Test
    public void testForChoose(){
        SqlSessionFactory factory = getSessionFactory();
        SqlSession session = factory.openSession();
        UserMapperDynamic userMapper = session.getMapper(UserMapperDynamic.class);
        User userCondition = new User();
        userCondition.setName("wangrui");
        userCondition.setAge(23);
        List<User> userList = userMapper.chooseCondition(userCondition);
        System.out.println(userList);
    }

    @Test
    public void testForSet(){
        SqlSessionFactory factory = getSessionFactory();
        SqlSession session = factory.openSession();
        UserMapperDynamic userMapper = session.getMapper(UserMapperDynamic.class);
        User userCondition = new User();
        userCondition.setPwd("123456");
        userCondition.setExeName("wr");
        userCondition.setId(3);
        int result = userMapper.updateSet(userCondition);
        System.out.println(result);
        session.commit();
        session.close();
    }
}
